package com.example.wemedia.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.util.R;
import com.example.model.wemedia.dto.MaterialDto;
import com.example.model.wemedia.pojo.Material;
import com.example.utils.thread.UserThreadLocal;
import com.example.wemedia.service.MaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "素材管理信息")
@RestController
@RequestMapping("/api/v1/")
public class MaterialController  {
    @Autowired
    private MaterialService materialService;

    @PostMapping(value = "/material/getAllMaterial/{page}/{pageSize}")
    @Operation(summary = "素材查询分页")
    public R getMaterialPage(@RequestBody MaterialDto materialDto, @PathVariable(value = "page") Long page, @PathVariable(value = "pageSize") Long pageSize) {
        // 分页功能进行查询
        Page< Material > materialPage = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper< Material > wrapper = new LambdaQueryWrapper<>();
        if (materialDto.getIsCollection() != null && materialDto.getIsCollection() ==1) {
            wrapper.eq(Material::getIsCollection, materialDto.getIsCollection());
        }
        wrapper.eq(Material::getUserId, UserThreadLocal.getUser().getUserId());
        // 按时间倒序
        wrapper.orderByAsc(Material::getCreatedTime);
        // 查询数据
        materialService.page(materialPage, wrapper);
        // 每页的数据集合
        List< Material > materialList = materialPage.getRecords();
        // 总的记录数
        Long total = materialPage.getTotal();
        // 每页条数
        Long size = materialPage.getSize();
        // 总页数
        Long pages = materialPage.getPages();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("pageSize", size);
        map.put("pages", pages);
        map.put("page", page);
        map.put("materialList", materialList);
        if (map != null) {
            return R.ok("查询素材分页成功").data(map);
        } else {
            return R.error("查询素材分页失败");
        }
    }

    @PostMapping(value = "/material/upload_picture")
    @Operation(summary = "上传图片")
    public R uploadPicture(MultipartFile multipartFile) {
        String url = materialService.uploadPicture(multipartFile);
        if (url != null) {
            return R.ok("上传图片成功").data("url", url);
        } else {
            return R.error("上传图片失败");
        }
    }

    /*@GetMapping("/getInfo/{id}")
    @Operation(summary = "按照ID编号查看")
    public ResultBean<Material> selectOne(@PathVariable Serializable id) {
        return ResultBean.success(this.materialService.getById(id));
    }

    @PostMapping("/save")
    @Operation(summary = "增加Material")
    public ResultBean insert(@RequestBody Material material) {
        this.materialService.save(material);
        return ResultBean.success();
    }

    @PutMapping("/update")
    @Operation(summary = "修改Material")
    public ResultBean update(@RequestBody Material material) {
        this.materialService.updateById(material);
        return ResultBean.success();
    }

    @DeleteMapping("/remove")
    @Operation(summary = "删除Material")
    public ResultBean delete(@RequestParam("idList") List<Long> idList) {
        this.materialService.removeByIds(idList);
        return ResultBean.success();
    }*/
}

